package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")

public class CustomerReservationController extends BaseController {


    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ReservationMapper reservationMapper;


    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult getCancelledReservationCount() {
        //后面开发了取消预约后，需要到数据库中去统计取消次数
        List<Reservation> reservations = reservationMapper.cancelCount(LocalDateTime.now(), UserThreadLocal.getUserId());
        int size = reservations.size();
        return success(size);
    }

    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult add(@RequestBody ReservationDto dto) {
        return reservationService.add(dto);
    }

    @GetMapping("/page")
    public ResponseResult<Reservation> queryByPage(@RequestParam Integer pageNum,
                                                   @RequestParam Integer pageSize,
                                                   @RequestParam(required = false) Integer status) {
        Long userId = UserThreadLocal.getUserId();
        PageResponse<Reservation> query = reservationService.queryByPage(pageNum, pageSize, status, userId);
        return success(query);
    }

    @PutMapping("/{id}/cancel")
    public ResponseResult updateStatus(@PathVariable("id") Long id) {
        Reservation reservation = new Reservation();
        reservation.setId(id);
        return reservationService.updateStatus(reservation);
    }
    @GetMapping("/countByTime")
    public ResponseResult countByTime(@RequestParam(required = false) Long time) {
        List<TimeCountVo> list = reservationService.countByTime(LocalDateTimeUtil.of(time));
        return success(list);
    }

}
